package cn.iocoder.yudao.module.crm.controller.admin.customFieldHead;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.mybatis.core.enums.QueryFieldType;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.crm.controller.admin.customFieldHead.vo.*;
import cn.iocoder.yudao.module.crm.dal.dataobject.customFieldHead.CustomFieldHeadDO;
import cn.iocoder.yudao.module.crm.service.customFieldHead.CustomFieldHeadService;

@Tag(name = "管理后台 - 自定义表头")
@RestController
@RequestMapping("/crm/custom-field-head")
@Validated
public class CustomFieldHeadController {

    @Resource
    private CustomFieldHeadService customFieldHeadService;

    @PostMapping("/create")
    @Operation(summary = "创建自定义表头")
    @PreAuthorize("@ss.hasPermission('crm:module:create')")
    public CommonResult<Long> createCustomFieldHead(@Valid @RequestBody CustomFieldHeadSaveReqVO createReqVO) {
        return success(customFieldHeadService.createCustomFieldHead(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新自定义表头")
    @PreAuthorize("@ss.hasPermission('crm:module:update')")
    public CommonResult<Boolean> updateCustomFieldHead(@Valid @RequestBody CustomFieldHeadSaveReqVO updateReqVO) {
        customFieldHeadService.updateCustomFieldHead(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除自定义表头")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:module:delete')")
    public CommonResult<Boolean> deleteCustomFieldHead(@RequestParam("id") Long id) {
        customFieldHeadService.deleteCustomFieldHead(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得自定义表头")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CustomFieldHeadRespVO> getCustomFieldHead(@RequestParam("id") Long id) {
        CustomFieldHeadDO customFieldHead = customFieldHeadService.getCustomFieldHead(id);
        return success(BeanUtils.toBean(customFieldHead, CustomFieldHeadRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得自定义表头分页")
    public CommonResult<PageResult<CustomFieldHeadRespVO>> getCustomFieldHeadPage(@Valid CustomFieldHeadPageReqVO pageReqVO) {
        PageResult<CustomFieldHeadDO> pageResult = customFieldHeadService.getCustomFieldHeadPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CustomFieldHeadRespVO.class));
    }
    @GetMapping("/list")
    @Operation(description = "获得自定义表头列表")
    public CommonResult<List<CustomFieldHeadDO>> getAgentList(@Valid CustomFieldHeadPageReqVO pageReqVO) {
        List<CustomFieldHeadDO> agentList = customFieldHeadService.getCustomFieldHeadList(pageReqVO);
        if (CollUtil.isNotEmpty(agentList)) {
            for (CustomFieldHeadDO customFieldHeadDO : agentList) {
                if (customFieldHeadDO.getFieldType().equals(QueryFieldType.INPUT)) {
                    customFieldHeadDO.setQueryType(QuerySceneBaseVO.buildDefaultQueryType());
                }
                if (customFieldHeadDO.getFieldType().equals(QueryFieldType.SELECT)) {
                    customFieldHeadDO.setQueryType(QuerySceneBaseVO.buildDefaultQueryType2());
                }
                if (customFieldHeadDO.getFieldType().equals(QueryFieldType.NUMBER)) {
                    customFieldHeadDO.setQueryType(QuerySceneBaseVO.buildRangeQueryType());
                }
                if (customFieldHeadDO.getFieldType().equals(QueryFieldType.TIME)) {
                    customFieldHeadDO.setQueryType(QuerySceneBaseVO.buildRangeQueryType());
                }
                if (customFieldHeadDO.getFieldType().equals(QueryFieldType.AREA)) {
                    customFieldHeadDO.setQueryType(QuerySceneBaseVO.buildDefaultQueryType2());
                }
            }
        }
        return success(BeanUtils.toBean(agentList, CustomFieldHeadDO.class));
    }


}